// 查询id为10的节点，输出节点路径如[1, 3, 10]
const treeData = [{
  id: 1,
  name: 'jj1',
  children: [
    { id: 2, name: 'jj2', children: [{ id: 4, name: 'jj4', }] },
    {
      id: 3,
      name: 'jj3',
      children: [
        { id: 8, name: 'jj8', children: [{ id: 5, name: 'jj5', }] },
        { id: 9, name: 'jj9', children: [] },
        { id: 10, name: 'jj10', children: [] },
      ],
    },
  ],
}];
let path = findNum(10, treeData);
console.log("path", path);


function findPath(target,treeData){
  let res = []
  const DG=(path,treeData)=>{
    if(!treeData.length) return
    treeData.forEach(item=>{
      path.push(item.id)
      if(item.id===target){
       res = JSON.parse(JSON.stringify(path))
      }else{
        const children=item.children ||[]
        DG(target,children)
        path.pop()
      }
    })
  }
  DG([],treeData)
  return res
}
// 实现
/**
 * 
 * @param {*} target 
 * @param {*} data 
 * @returns 
 * 
 * const findNum = (target, data) => {
  let result = [];
  const DG = (path, data) => {
    if (!data.length) return
    data.forEach(item => {
      path.push(item.id)
      if (item.id === target) {
        result = JSON.parse(JSON.stringify(path));
      } else {
        const children = item.children || [];
        DG(path, children);
        path.pop();
      }
    })
  }
  DG([], data)
  return result
};
 */
